package com.sunmouse.toollib.utils.system;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.sunmouse.toollib.utils.StringUtils;
import com.sunmouse.toollib.utils.ToastUtils;

/**
 * Created by Kvin on 2017/4/28.
 */
public class PermissionUtil {
    /**
     * check permission
     */
    public static boolean isPermitted(Context context, String permission) {
        if (StringUtils.isEmpty(permission)) {
            throw new RuntimeException("permission cannot be null");
        }
        int status = ContextCompat.checkSelfPermission(context, permission);
        return status == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * check a related group permission
     *
     * @param context
     * @param permission
     * @return
     */
    public static boolean isPermitted(Context context, String[] permission) {
        if (permission == null || permission.length <= 0) {
            throw new RuntimeException("permission cannot be null");
        }
        boolean per = true;
        for (int i = 0; i < permission.length; i++) {
            int status = ContextCompat.checkSelfPermission(context, permission[i]);
            per = per && (status == PackageManager.PERMISSION_GRANTED);
        }
        return per;
    }

    /**
     * check whether gps is open
     */
    public static void checkGPS(Activity act) {
        LocationManager manager = (LocationManager) act.getSystemService(Context.LOCATION_SERVICE);
        boolean isGPSEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!isGPSEnabled) {
//            manager.s
            ToastUtils.simpleToast(act, "请开启GPS");
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            act.startActivity(intent);
        }
    }

    /**
     * is call permitted
     */
    public static boolean isCallPermitted(Activity act, int requestCode) {
        boolean result = isPermitted(act, CALL_PER);
        if (!result) requestPermission(act, CALL_PER, requestCode);
        return result;
    }

    /**
     * is camera permitted
     */
    public static boolean isCameraPermitted(Activity act, int requestCode) {
        boolean result = isPermitted(act, CAMERA_PER);
        if (!result) requestPermission(act, CAMERA_PER, requestCode);
        return result;
    }

    /**
     * is storage permitted
     */
    public static boolean isStoragePermitted(Activity act, int requestCode) {
        boolean result = isPermitted(act, STORAGE_PER);
        if (!result) requestPermission(act, STORAGE_PER, requestCode);
        return result;
    }

    /**
     * is Location permitted
     */
    public static boolean isLocationPermitted(Activity act, int requestCode) {
        boolean result = isPermitted(act, LOCATION_PER);
        if (!result) requestPermission(act, LOCATION_PER, requestCode);
        return result;
    }
    /**
     * is reading contracts permitted
     */
    public static boolean isReadingContractPermitted(Activity act, int requestCode) {
        boolean result = isPermitted(act, CONTRACT_PER);
        if (!result) requestPermission(act, CONTRACT_PER, requestCode);
        return result;
    }


    /**
     * request permission
     * requestCode use for handling recall msg
     */
    public static void requestPermission(Activity activity, String permission, int requestCode) {
        ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
    }

    public static void requestPermission(Activity activity, String[] permission, int requestCode) {
        ActivityCompat.requestPermissions(activity, permission, requestCode);
    }

    /**
     * call permission
     */
    public final static String[] CALL_PER = {
            Manifest.permission.CALL_PHONE
    };

    /**
     * camera permissions
     */
    public final static String[] CAMERA_PER = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
    };

    /**
     * access storage permissions
     */
    public final static String[] STORAGE_PER = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    /**
     * access location permission
     */
    public final static String[] LOCATION_PER = {
//            Manifest.permission.LOCATION_HARDWARE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
    };

    /**
     * access contracts
     */
    public final static String[] CONTRACT_PER = {
            Manifest.permission.READ_CONTACTS
    };


}
